package com.hivekion.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.system.domain.SysOperationLog;
import com.hivekion.system.domain.vo.SysOperationLogModelVo;
import com.hivekion.system.domain.vo.SysOperationLogSearchPageInputVo;
import com.hivekion.system.domain.vo.SysOperationLogViewVo;
import com.hivekion.system.mapper.SysOperationLogMapper;
import com.hivekion.system.service.ISysOperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysOperationLogServiceImpl implements ISysOperationLogService {
      @Autowired
      SysOperationLogMapper mapper;

      @Override
      public void insertOperationLog(SysOperationLog log) {
	    mapper.insert(log);
      }

      @Override
      public boolean remove(String operId) {
	    return mapper.deleteById(operId) > 0;
      }

      @Override
      public void cleanOperationLog() {
	    mapper.cleanOperationLog();
      }

      @Override
      public PagedResultVo<SysOperationLogViewVo> list(SysOperationLogSearchPageInputVo inputVo) {
	    LambdaQueryWrapper<SysOperationLog> logLambdaQueryWrapper = Wrappers.lambdaQuery();
	    if (StringUtils.isNotEmpty(inputVo.getFilter())) {
		  logLambdaQueryWrapper.and(
			  t ->
				  t.like(SysOperationLog::getMethod, inputVo.getFilter())
					  .or()
					  .like(SysOperationLog::getTitle, inputVo.getFilter()));
	    }
	    if (inputVo.getStartTime() != null) {
		  logLambdaQueryWrapper.and(
			  t -> t.gt(SysOperationLog::getCreateTime, inputVo.getStartTime()));
	    }
	    if (inputVo.getEndTime() != null) {
		  logLambdaQueryWrapper.and(
			  t -> t.lt(SysOperationLog::getCreateTime, inputVo.getEndTime()));
	    }
	    if (inputVo.getLevel() != null && inputVo.getLevel() != -1) {
		  logLambdaQueryWrapper.and(t -> t.eq(SysOperationLog::getLevel, inputVo.getLevel()));
	    }
	    if (inputVo.getStatus() != null && inputVo.getStatus() != -1) {
		  logLambdaQueryWrapper.and(t -> t.eq(SysOperationLog::getStatus, inputVo.getStatus()));
	    }
	    Page<SysOperationLog> logPage = new Page<>(inputVo.getPageNum(), inputVo.getPageSize());
	    logPage.addOrder(new OrderItem("create_time", false));
	    IPage<SysOperationLog> logPageResult = mapper.selectPage(logPage, logLambdaQueryWrapper);
	    List<SysOperationLogViewVo> data =
		    logPageResult.getRecords().stream()
			    .map(
				    item -> {
					  SysOperationLogViewVo vo = new SysOperationLogViewVo();
					  BeanUtils.copyProperties(item, vo);
					  return vo;
				    })
			    .collect(Collectors.toList());
	    PagedResultVo<SysOperationLogViewVo> resultVo =
		    new PagedResultVo<SysOperationLogViewVo>(
			    inputVo, (int) logPageResult.getTotal(), data);
	    return resultVo;
      }

      @Override
      public SysOperationLogModelVo getInfo(String id) {
	    SysOperationLog info = mapper.selectById(id);
	    SysOperationLogModelVo vo = new SysOperationLogModelVo();
	    BeanUtils.copyProperties(info, vo);
	    return vo;
      }
}
